package org.ipower.examsystem.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.ipower.examsystem.dao.CatalogDao;
import org.ipower.examsystem.domain.Catalog;
import org.ipower.examsystem.domain.DataField;
import org.ipower.platform.service.TreeData;
import org.ipower.platform.service.TreeDataCollection;
/**
 * 考试科目数据访问实现类。
 * @author jeasonyoung
 * */
public class CatalogDaoImpl  extends ModuleGenericDaoImpl<Catalog> implements CatalogDao  {
    /**
    * 获取不包括指定考试科目ID子目录所有子科目集合.
    * @param catalogID 考试科目ID.
    * @return 子科目集合.
    * */
    @Override
    public List<DataField> NotSelfCatalog(String catalogID) {
        List<DataField> result = null;
        List<Object> list = this.namedQuery("getNotSelfCatalog", new String[]{"FieldValue"}, new Object[]{catalogID});
        if(list != null && list.size() > 0){
            result = new ArrayList<DataField>();
            for(int i = 0; i < list.size(); i++){
                Object obj = list.get(i);
                if(obj != null && (obj instanceof DataField)){
                    result.add((DataField)obj);
                }
            }
        }
       return result;
    }
    /**
     * 获取考试科目树形结构数据。
     * @return 树形结构数据。
     * */
    @Override
    public TreeDataCollection getCatalogs() {
        TreeDataCollection collection = new TreeDataCollection();
        List<Catalog> catalogs = this.loadAll();
        if(catalogs != null && catalogs.size() > 0){
            List<Catalog> removes = new ArrayList<Catalog>();
            for(int i = 0; i < catalogs.size(); i++){
                Catalog c = catalogs.get(i);
                if(c.getParentCatalogID() == null || c.getParentCatalogID().isEmpty()){
                    TreeData data = new TreeData();
                    data.setID(c.getCatalogID());
                    data.setText(c.getCatalogName());
                    data.setLeaf(true);
                    collection.add(data);
                    removes.add(c);
                }
            }
            if(collection.size() > 0){
                catalogs.removeAll(removes);
                for(int i = 0; i < collection.size(); i++){
                    this.createTree(collection.get(i), catalogs);
                }
            }
        }
        return collection;
    }
    /**
     * 创建树结构。
     * @param parent 父节点.
     * @param list 数据集。
     * */
    private void createTree(TreeData parent, List<Catalog> catalogs){
        if(parent != null){
            List<Catalog> childs = this.getChilds(catalogs,parent.getID());
            if(childs != null && childs.size() > 0){
                catalogs.removeAll(childs);
                for(int i = 0; i< childs.size(); i++){
                    Catalog c = childs.get(i);
                    TreeData data = new TreeData();
                    data.setID(c.getCatalogID());
                    data.setText(c.getCatalogName());
                    data.setLeaf(true);
                    this.createTree(data, catalogs);                    
                    parent.setLeaf(false);
                    parent.setExpanded(true);
                    parent.getChildren().add(data);
                }
            }
        }
    }
    /**
     * 获取所有子目录结构。
     * @param parentId 父目录ID。
     * @param list 源数据集合。
     * */
    private List<Catalog> getChilds(List<Catalog> catalogs,String parentId){
        List<Catalog> result = new ArrayList<Catalog>();
        if(catalogs != null && catalogs.size() > 0){
            for(int i = 0; i < catalogs.size(); i++){
                Catalog c = catalogs.get(i);
                if(c.getParentCatalogID().equalsIgnoreCase(parentId)){
                    result.add(c);
                }
            }
        }
        return result;
    }
}